﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using ChinaSoft.BLL;
using ChinaSoft.Model;

namespace Admin.Sys.User
{
    public partial class User_Edit : System.Web.UI.Page
    {
        private BLL_T_SYS_USER bllUser = new BLL_T_SYS_USER();
        private Model_T_SYS_USER modelUser = new Model_T_SYS_USER();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTreeView();
                ControlBtn(false, false, false, false, false);
                ControlTextBox(true, true,true,true,true,true,true,true);
                BindGroup();
            }
        }
        /// <summary>
        /// 控制控件的状态（禁用、启用）
        /// </summary>
        /// <param name="btnAdd"></param>
        /// <param name="btnDel"></param>
        /// <param name="btnUpdate"></param>
        /// <param name="btnSave"></param>
        /// <param name="btnCancel"></param>
        private void ControlBtn(bool btnAdd, bool btnDel, bool btnUpdate, bool btnSave, bool btnCancel)
        {
            imgBtnAdd.Enabled = btnAdd;
            imgBtnDel.Enabled = btnDel;
            imgBtnUpdate.Enabled = btnUpdate;
            imgBtnSave.Enabled = btnSave;
            imgBtnCancel.Enabled = btnCancel;
        }
        /// <summary>
        /// 绑定树性菜单
        /// </summary>
        private void BindTreeView()
        {
            TreeView1.Nodes.Clear();
            DataSet ds = bllUser.GetAllList();
            ChinaSoft.BLL.Common.TreeViewOperate bllTreeView = new ChinaSoft.BLL.Common.TreeViewOperate();
            bllTreeView.BindTreeView(ds, TreeView1, "NAME", "sys_user_id", false);
        }
        /// <summary>
        /// 当树形菜单选择改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            TreeViewChanged(TreeView1.SelectedNode.Value);
        }
        private void TreeViewChanged(string seLectedValue)
        {
            ControlBtn(true, true, true, false, false);
            modelUser = bllUser.GetModel(seLectedValue);
            if (modelUser != null)
            {
                txtName.Value = modelUser.NAME;
                txtLoginName.Value = modelUser.LOGIN_NAME;
                txtProblem.Value = modelUser.PWD_QUESTION;
                txtAnswer.Value = modelUser.PASSWORD_ANSWER;
                txtGroupName.Value = modelUser.GROUP_NAME;
                HiddenUserId.Value = modelUser.SYS_USER_ID;
                HiddenGroupId.Value = modelUser.GROUP_ID;
            }

            ControlTextBox(true, true,true,true,true,true,true,false);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
        {
            ControlBtn(true, true, false, true, true);
            ControlTextBox(false, false,false,false,false,false,false,true);
            HiddenUserId.Value = "";
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnDel_Click(object sender, ImageClickEventArgs e)
        {
            string roleId = ChinaSoft.Common.Input.SetString(TreeView1.SelectedNode.Value);
            bool result = bllUser.Delete(roleId);
            if (result)
            {
                ChinaSoft.Common.MessageBox.Show(this, "删除成功！");
                ControlTextBox(false, false,false,false,false,false,false,true);
                BindTreeView();
                HiddenUserId.Value = "";
            }
            else
            {
                ChinaSoft.Common.MessageBox.Show(this, "该数据已经不存在");
                BindTreeView();
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnUpdate_Click(object sender, ImageClickEventArgs e)
        {
            ControlBtn(true, true, false, true, true);
            ControlTextBox(false, false, false, false, false, false, false, false);
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
        {
            if (HiddenUserId.Value == "")
            {
                #region 添加
                modelUser.SYS_USER_ID = Guid.NewGuid().ToString();
                modelUser.NAME = txtName.Value.Trim();
                modelUser.LOGIN_NAME = txtName.Value.Trim();
                modelUser.PASSWORD = txtPwd.Value.Trim();
                modelUser.PWD_QUESTION = txtProblem.Value.Trim();
                modelUser.PASSWORD_ANSWER = txtAnswer.Value.Trim();

                modelUser.CREATE_TIME = DateTime.Now;
                modelUser.CREATOR_ID = ChinaSoft.Common.Input.SetString(Session["currentUserID"]);
                modelUser.CREATOR_NAME = ChinaSoft.Common.Input.SetString(Session["UserName"]);
                modelUser.NAME = txtName.Value.Trim();
                modelUser.UPDATE_TIME = DateTime.Now;
                modelUser.UPDATOR_ID = ChinaSoft.Common.Input.SetString(Session["currentUserID"]);
                modelUser.UPDATOR_NAME = ChinaSoft.Common.Input.SetString(Session["UserName"]);
                bllUser.Add(modelUser);
                #endregion
                ChinaSoft.Common.MessageBox.Show(this, "添加成功！");
                BindTreeView();
                ControlTextBox(true, true, true, true, true, true, true, true);
            }
            else
            {
                modelUser = bllUser.GetModel(HiddenUserId.Value);
                #region 更新
                if (string.IsNullOrEmpty(modelUser.CREATOR_ID))
                    modelUser.CREATOR_ID = "";
                if (string.IsNullOrEmpty(modelUser.CREATOR_NAME))
                    modelUser.CREATOR_NAME = "";
                modelUser.NAME = txtName.Value.Trim();
                modelUser.LOGIN_NAME = txtLoginName.Value.Trim();
                modelUser.PWD_QUESTION = txtProblem.Value.Trim();
                modelUser.PASSWORD_ANSWER = txtAnswer.Value.Trim();
                modelUser.PASSWORD = txtPwd.Value.Trim();

                modelUser.UPDATE_TIME = DateTime.Now;
                modelUser.UPDATOR_ID = ChinaSoft.Common.Input.SetString(Session["currentUserID"]);
                modelUser.UPDATOR_NAME = ChinaSoft.Common.Input.SetString(Session["UserName"]);
                bllUser.Update(modelUser);

                #endregion
                ChinaSoft.Common.MessageBox.Show(this, "更新成功！");
                BindTreeView();
            }
        }
        /// <summary>
        /// 绑定组信息
        /// </summary>
        private void BindGroup()
        {
            TreeView2.Nodes.Clear();
            BLL_T_SYS_GROUP bllGroup = new BLL_T_SYS_GROUP();
            DataSet ds = bllGroup.GetAllList();
            ChinaSoft.BLL.Common.TreeViewOperate bllTreeView = new ChinaSoft.BLL.Common.TreeViewOperate();
            bllTreeView.BindTreeView(ds, TreeView2, "NAME", "SYS_GROUP_ID", "PARENT_ID", false);
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnCancel_Click(object sender, ImageClickEventArgs e)
        {
            ControlBtn(true, true, true, false, false);
            ControlTextBox(true, true, true, true, true, true, true, true);
        }

        /// <summary>
        /// 控制文本框
        /// </summary>
        /// <param name="txtNameDisabled"></param>
        /// <param name="boolClear"></param>
        private void ControlTextBox(bool txtNameDisabled,bool txtLoginDisabled,bool txtProblemDisabled,bool txtAnswerDisabled,bool txtPwdDisabled,bool txtRePwdDisabled,bool txtGroupNameDisabled, bool boolClear)
        {
            if (boolClear)
            {
                txtName.Value = "";
                txtLoginName.Value = "";
                txtProblem.Value = "";
                txtAnswer.Value = "";
                txtPwd.Value = "";
                txtRePwd.Value = "";
                txtGroupName.Value = "";
            }
            txtName.Disabled = txtNameDisabled;
            txtLoginName.Disabled = txtLoginDisabled;
            txtProblem.Disabled = txtProblemDisabled;
            txtAnswer.Disabled = txtAnswerDisabled;
            txtPwd.Disabled = txtPwdDisabled;
            txtRePwd.Disabled = txtRePwdDisabled;
            txtGroupName.Disabled = txtGroupNameDisabled;
        }

        protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
        {
            TreeViewChanged(e.Node.Value);
        }

        protected void TreeView1_TreeNodeCollapsed(object sender, TreeNodeEventArgs e)
        {
            TreeViewChanged(e.Node.Value);
        }

        protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
        {
            TreeViewChanged(e.Node.Value);
        }

        protected void TreeView2_SelectedNodeChanged(object sender, EventArgs e)
        {
            int groupId = ChinaSoft.Common.Input.SetInt(TreeView2.SelectedNode.Value);
            
            HiddenGroupId.Value = TreeView2.SelectedNode.Value;
            HiddenGroupName.Value = TreeView2.SelectedNode.Text;
        }
    }
}